package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Project;
import cn.wolfcode.crm.mapper.ProjectMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IProjectService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ProjectServiceImpl implements IProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        projectMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Project selectByPrimaryKey(Long id) {
        return projectMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Project> selectAll() {
        return projectMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Project entity) {
        if (entity.getId() != null){
            projectMapper.updateByPrimaryKey(entity);
        }else {
            Subject subject = SecurityUtils.getSubject();
            Employee principal = (Employee) subject.getPrincipal();
            entity.setInput(principal);
            projectMapper.insert(entity);
        }
    }
    @Override
    public PageResult query(QueryObject qo) {
        int total = projectMapper.query4Count(qo);
        if(total == 0 ){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<?> rows = projectMapper.query4List(qo);
        return new PageResult(total,rows);
    }

    @Override
    public void changeReport(Project entity) {
        projectMapper.changeReport(entity);
    }


}
